import { getTemplateData } from '../../_utils/tool'
import $copy from '@utils/copy'
import onInit from '../../_utils/edit'

const { $fn, $ } = window

export const Index = ({ formRef, template, folder }) => {
	const { table_name, cols, table_comment, temp } = $.ref(formRef).getValue()
	if( !table_name ) return $fn.msg.warning('表名不能为空')
	if( !template ) return $fn.msg.warning('模板不能为空')
	const _template = temp ? temp : getTemplateData( { template, table_name, cols, table_comment } )
	window.$formModalRef().open({
		modal: {
			title 		: '预览表',
			width 		: '100%',
			height 		: '100%',
			maxWidth 	: null,
			controls: {
				after:[
					{ label:'复制表', ghost:1, click: () => $copy(window.$formModalRef().formRef().getValue('template')) },
				],
				ok: { label: '生成建表文件' }
			}
		},
		form: {
			api 		: 'file/create_table',
			labelWidth 	: 's10',
			className 	: 'pre-form',
			okTxt 		: `表【${table_name}】生成成功`,
			param : { folder, table_name, template:_template },
			data: [
				{ name:'template', type:'textarea', maxLength: null},
			],
			onInit: () => onInit()
		}
	})
}
export default Index